<?php

//by ralvear 20131024
class historicocarteraxmesControlador extends Controlador 
{
	
	public function __construct()
	{
		parent::__construct();
		$this->_vista->asignartJs( array( 'historicocarteraxmes' ) );
		$this->_historicocarteraDAO = $this->cargarModelo( "historicocarteraxmes" );
	}

	public function index()
	{
		//Sesion::accesoControlado( array( 'usuario' ) );
		$this->_vista->titulo = 'Bienvenido...';		
		$this->_vista->render('index');
	}

	public function cargarlista() {
		$lista = $this->obtenerVcadena( "lista" );
		$filtro = $this->obtenerVcadena( "filtro" );
		$esMultiple = $this->obtenerVcadena( "esMultiple" );

	    try {
	        $lstRet = $this->_historicocarteraDAO->cargarlista($lista, $filtro, $esMultiple);

	        if(isset($lstRet)) {
		        echo json_encode(array(
		                    'success' => TRUE,
		                    'lista_ret' => $lstRet
		                ));
	        }
	    } catch (Exception $e) {
	        echo "ERROR->" . $e;
	        echo "<option value='-1'>Error</option>";
	    }
	}

	public function consultarHistoricoCarteraxMes() {

		$fecha_inicial = $this->obtenerVcadena( "fecha_inicial" );
		$fecha_final = $this->obtenerVcadena( "fecha_final" );
		$almacen = $this->obtenerVcadena( "almacen" );
		$linea = $this->obtenerVcadena( "linea" );
		$cliente = $this->obtenerVcadena( "cliente" );
		$departamento = $this->obtenerVcadena( "departamento" );
		$producto = $this->obtenerVcadena( "producto" );

		$jsonCate = array();
		$jsonSeriesCan = array();
		$jsonSeriesDin = array();


		$jsonData = array();

		try {
	        $lstCarteraAlmacen = $this->_historicocarteraDAO->consultarHistoricoCarteraxMes($fecha_inicial, $fecha_final, $almacen, $linea, $cliente, $departamento, $producto);

	        if( isset($lstCarteraAlmacen) ) {

	        	$rg_CAlamacen;
	        	
	        	for( $i = 0; $i < count($lstCarteraAlmacen); $i++){

					$rg_CAlamacen = $lstCarteraAlmacen[$i];

					 //series: [ {name: 'Tokyo',data: [7.0, 6.9, 9.5, 14.5, 18.2, 21.5, 25.2, 26.5, 23.3, 18.3, 13.9, 9.6] }
					$jsonDataSeries[$rg_CAlamacen["sucursalnombre"]][] = (double)$rg_CAlamacen["detalle_facturavalortotal"];

					if( isset($jsonCategorias) ) $array_key = array_flip($jsonCategorias);

					if( isset($array_key) ){

						if(is_array($array_key)){

							if(!array_key_exists($rg_CAlamacen["facturafecha"], $array_key)){
								$jsonCategorias[] = $rg_CAlamacen["facturafecha"];
							}
						}
					}else{
						$jsonCategorias[] = $rg_CAlamacen["facturafecha"];
					}				

	            }	

	            foreach ($jsonDataSeries as $clave => $valor) {

    				$jsonSeries[] = array("name" => $clave, "data" => $valor);
				}


				echo (json_encode(
					array(
						"categories" => $jsonCategorias,
						"series" => $jsonSeries
					)
				));

	        }

	    } catch (Exception $e) {
	        echo "ERROR->" . $e;
	        echo "<option value='-1'>Error</option>";
	    }

	}

}

?>